package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsSearchHistoryMapper;
import com.shop.pojo.DtsCoupon;
import com.shop.pojo.DtsSearchHistory;
import com.shop.service.DtsSearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsSearchHistoryServiceImpl
 * @Author xiao宋
 * @Date 15:23  2022/10/22
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsSearchHistoryServiceImpl implements DtsSearchHistoryService {
    @Autowired
    DtsSearchHistoryMapper historyMapper;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        QueryWrapper<DtsSearchHistory> w = new QueryWrapper<>();
        IPage<DtsSearchHistory> iPage = new Page<>(page, limit);
        if (userId != null) {
            w.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(keyword)) {
            w.eq("keyword", keyword);
        }
//        w.eq("deleted", false);
        //        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc( sort);
        } else {
            w.orderByAsc(sort);
        }

        historyMapper.selectSearchHistoryPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }
}
